package com.spdzen.controller.admin;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.model.SysDept;
import com.spdzen.service.SysDeptService;
import com.spdzen.support.JsonUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.exception.ResultCodeException;
import com.spdzen.support.result.JsonResult;
import com.spdzen.support.result.ResultCode;
import com.spdzen.support.validation.AddValidGroup;
import com.spdzen.support.validation.UpdateValidGroup;
import com.spdzen.vo.search.SysDeptAdminSearch;

@Controller
@RequestMapping("/admin/sysdept")
public class SysDeptAdminController {
	@Resource
	private SysDeptService sysDeptService;

	@RequestMapping("/pageView.do")
	public String pageView() {
		return "/admin/sysdept/sysDeptPageView";
	}

	@ResponseBody
	@RequestMapping("/queryPage.do")
	public JsonResult queryPage(PageOffsetParam param, SysDeptAdminSearch search) {
		try {
			return JsonResult.success(sysDeptService.selectPage(param, search));
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(param, search), e);
			return JsonResult.error();
		}
	}
	
	@ResponseBody
	@RequestMapping("/queryForTree.do")
	public JsonResult queryForTree() {
		try {
			return JsonResult.success(sysDeptService.selectAll());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}

	@ResponseBody
	@RequestMapping("/add.do")
	public JsonResult add(@Validated(value = { AddValidGroup.class }) SysDept entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return JsonResult.errorParameter();
		}
		
		try {
			sysDeptService.inserIfPermission(entity);
			return JsonResult.success();
		} catch (ResultCodeException e) {
			return JsonResult.error(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(entity), e);
			return JsonResult.error();
		}
	}
	
	@ResponseBody
	@RequestMapping("/update.do")
	public JsonResult update(@Validated(value = { UpdateValidGroup.class }) SysDept entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return JsonResult.errorParameter();
		}
		
		try {
			sysDeptService.updateIfPermission(entity);
			return JsonResult.success();
		} catch (ResultCodeException e) {
			return JsonResult.error(e.getResultCode());
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(entity), e);
			return JsonResult.error();
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete.do")
	public JsonResult delete(Long id) {
		if (id == null || id.longValue() < 1L) {
			return JsonResult.errorParameter();
		}
		
		try {
			ResultCode rc = sysDeptService.deleteIfPermission(id);
			return new JsonResult(rc);
		} catch (Exception e) {
			LogUtils.getLogger().error(id.toString(), e);
			return JsonResult.error();
		}
	}
}
